﻿using Beway.ValueAdded.Model;
using Beway.ValueAdded.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Beway.ValueAdded.Service
{
    public class SmsLogSvc
    {
        /// <summary>
        /// 发送充值超预警值短信
        /// </summary>
        /// <param name="sl"></param>
        /// <returns></returns>
        public bool SendAlarmSms(SmsLog sl)
        {
            bool ret = false;
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                DateTime lastSendTime = System.DateTime.Now.AddMinutes(-30);//30分钟前的时间

                //该项目30分钟内系统警告短信记录
                var list = db.SmsLog.Where(m => m.ReUserId == sl.ReUserId && m.SmsType == 0 && m.SendTime > lastSendTime);

                if (!list.Any()) //不存在
                {
                    ret = Add(sl);//发送
                }
            }
            return ret;
        }

        /// <summary>
        /// 发送短信并记录日志
        /// </summary>
        /// <param name="mobile">手机号</param>
        /// <param name="content">短信内容</param>
        /// <returns></returns>
        public bool Add(SmsLog sl)
        {
            int ret = 0;

            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                db.SmsLog.Add(sl);
                if (db.SaveChanges() > 0)
                {
                    if (this.SendSms(sl.Mobile, "【瀚微互动】" + sl.SmsContent))
                    {
                        sl.SendStatus = 1;//发送成功
                        ret = db.SaveChanges();
                    }
                }
            }
            return ret > 0;
        }

        /// <summary>
        /// 云掌通接口 短信发送 
        /// </summary>
        /// <param name="mobile">可同时发送单个或多个手机号（以逗号隔开，最多不超过5000个）</param>
        /// <param name="msgContent">短信内容</param>
        /// <returns></returns>
        private bool SendSms(string mobile, string msgContent)
        {
            
            bool result = beway.common.SMS.SendByYzt(mobile, msgContent);
            
            return result;
        }
    }

    /// <summary>
    /// 判断类型型枚举
    /// </summary>
    public enum SmsTypeEnum
    {
        /// <summary>
        /// 系统短信
        /// </summary>
        SysSms = 0,

        /// <summary>
        /// 活动短信
        /// </summary>
        UserSms = 1
    }
}
